home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 16 / IOPROG_16.ISO / soft / macaxsdk / macsdk.hqx / ActiveX DR3 SDK / ActiveX SDK / Sample Containers / SimpleContainer / Main.c / Main.c
Encoding:
C/C++ Source or Header  |  1997-08-20  |  8.8 KB  |  379 lines  |  [TEXT/CWIE]

  1. //
  2. //    Main.cpp
  3. //
  4. // >>> ⌐ 1996-1997 Microsoft Corporation.  All rights reserved. <<<
  5. //
  6.  
  7. #include "ActiveXAPI.h"
  8. #include "App.h"
  9. #include "Document.h"
  10. #include "Menus.h"
  11. #include "Event.h"
  12. #include "Callbacks.h"
  13. #include "Figure.h"
  14. #include "ObjectInfoWin.h"
  15. #include "TestCallbacks.h"
  16.  
  17. //    the one global variable
  18. AppData        gAppData = { NULL, NULL, NULL, NULL, false, false, false, NULL, kInvalidCursor, 0};
  19.  
  20. static Boolean    Initialize(void);
  21. static void        Shutdown(void);
  22.  
  23. #define DECLARE_UPP( routine, what ) \
  24.         RoutineDescriptor routine##_upp = BUILD_ROUTINE_DESCRIPTOR( \
  25.         upp##what##ProcInfo, routine )
  26.  
  27. #define UPP( routine, what ) \
  28.         (what##UPP) &routine##_upp
  29.  
  30. int    initae (void);
  31. static pascal OSErr
  32. DoAEOpenApplication
  33.     (AppleEvent    *theAppleEvent,
  34.      AppleEvent    reply,
  35.      long        refcon);
  36. static pascal OSErr
  37. DoAEQuitApplication
  38.     (AppleEvent    *theAppleEvent,
  39.      AppleEvent    reply,
  40.      long        refcon);
  41. static pascal OSErr
  42. DoAEOpenOrPrintDocuments
  43.     (AppleEvent    *theAppleEvent,
  44.      AppleEvent    reply,
  45.      long        refcon);
  46. static OSErr
  47. MissedAnyParameters
  48.     (const AppleEvent    *theAppleEvent);
  49.  
  50. /*****************************************************************************
  51. UPP Declarations
  52. *****************************************************************************/
  53. DECLARE_UPP (DoAEOpenApplication, AEEventHandler);
  54. DECLARE_UPP (DoAEOpenOrPrintDocuments, AEEventHandler);
  55. DECLARE_UPP (DoAEQuitApplication, AEEventHandler);
  56.  
  57. //
  58. //    main
  59. //
  60. void main(void)
  61. {
  62.     if (Initialize())
  63.     {
  64.         EventLoop();
  65.         Shutdown();
  66.     }
  67. }
  68.  
  69.  
  70. //
  71. //    Initialize
  72. //
  73. Boolean Initialize(void)
  74. {
  75.     Boolean    ReturnValue = false;
  76.     short    i;
  77.  
  78.     //    Mac Tool Initializition
  79.     InitGraf((Ptr) &qd.thePort);
  80.     InitFonts();
  81.     InitWindows();
  82.     InitMenus();
  83.     TEInit();
  84.     InitDialogs(0L);
  85.     InitCursor();
  86.     initae();
  87.  
  88.     // allocate master handles    
  89.     MaxApplZone();        
  90.     for (i=1; i<=10; ++i)
  91.         MoreMasters();
  92.  
  93.     SetupMenus(false); 
  94.  
  95.     // initialize ActiveX
  96.     {
  97.     #ifdef DR3_API
  98.         AXClientInfo        ClientInfo = {kAXArchCFMPPC, 0};
  99.         AXClientCallbacks    Callbacks = {nil};
  100.         
  101.         AXExplorerBasicCallbacks    IECallbacks =  { IEBasic_IEGotoNewURL, IEBasic_IELoadingStarted, IEBasic_IELoadingFinished, IEBasic_IETitleChanged,
  102.                                                     IEBasic_IEDocumentChanged, IEBasic_IEHandleURL };
  103.         AXURLStreamCallbacks        URLStreamCallbacks = { URLStream_OpenStream, URLStream_ReadContentType, URLStream_ReadData,
  104.                                                     URLStream_CloseStream, URLStream_MapURL };
  105.  
  106.     #else
  107.         AXCallbacks            Callbacks = {nil};
  108.     #endif
  109.         
  110.         AXSiteCallbacks        SiteCallbacks = { Site_RequestFocus, Site_RequestSizeChange,
  111.                                                 Site_OnChange, Site_AcquireContext, Site_ReleaseContext };
  112.         Callbacks.SiteCallbacks = &SiteCallbacks;
  113.         
  114.     #ifdef DR3_API
  115.         // DR3 API supports Explorer and URLStream callbacks
  116.         Callbacks.ExplorerBasicCallbacks = &IECallbacks;
  117.         Callbacks.URLStreamCallbacks = &URLStreamCallbacks;
  118.  
  119.         Callbacks.StructSize = sizeof(AXClientCallbacks) - 4;
  120.  
  121.         // Initialize ActiveX
  122.         ReturnValue = ( AX_RegisterClient(&ClientInfo, &Callbacks, &gAppData.ClientID) == S_OK );
  123.         
  124.     #else
  125.         
  126.         // Initialize ActiveX
  127.         ReturnValue = AX_Initialize(&Callbacks) == S_OK;
  128.     #endif
  129.     }
  130.  
  131.     return ReturnValue;
  132. }
  133.  
  134. //
  135. //    SetFocusFigure
  136. //
  137. void SetFocusFigure(FigureData* inFigure)
  138. {
  139.     gAppData.FocusFigure = inFigure;
  140.     if ( inFigure )
  141.         SetSelectedFigure(inFigure);
  142.  
  143.     gAppData.MenuBarDirty = true;
  144. }
  145.  
  146. //
  147. //    SetSelectedFigure
  148. //
  149. void SetSelectedFigure(FigureData* inFigure)
  150. {
  151.     if (inFigure != gAppData.SelectedFigure)
  152.     {
  153.         if (gAppData.SelectedFigure)
  154.             InvalFigure(gAppData.SelectedFigure);
  155.  
  156.         gAppData.SelectedFigure = inFigure;
  157.  
  158.         if ( gInfoWindow )
  159.         {
  160.             if ( inFigure )
  161.                 SetInfoSiteID(gInfoWindow, inFigure->Site);
  162.             else
  163.                 SetInfoSiteID(gInfoWindow, 0);
  164.         }
  165.  
  166.         gAppData.MenuBarDirty = true;
  167.     }
  168.  
  169.     if (inFigure)
  170.         InvalFigure(inFigure);
  171. }
  172.  
  173. //
  174. //    InvalFigure
  175. //
  176. void InvalFigure(FigureData* inFigure)
  177. {
  178.     GrafPtr SavePort;
  179.     Rect    RectErase = inFigure->ControlRect;
  180.     Rect    PortRect;
  181.     
  182.     GetPort(&SavePort);
  183.     SetPort(inFigure->Document->Window);
  184.  
  185.     //    may be called during a callback from a control - so offset first
  186.     PortRect = inFigure->Document->Window->portRect;
  187.     OffsetRect(&RectErase, PortRect.left, PortRect.top);
  188.     InsetRect(&RectErase, BorderWidth, BorderWidth);
  189.     InvalRect(&RectErase);
  190.  
  191.     SetPort(SavePort);
  192. }
  193.  
  194. //
  195. //    Shutdown
  196. //
  197. void Shutdown(void)
  198. {
  199.     WindowRef    Window;
  200.  
  201.     //    close down all windows
  202.     while ((Window = FrontWindow()) != NULL)
  203.     {
  204.         DoClose(Window);
  205.     }
  206.  
  207. //    close down ActiveX
  208. #ifdef DR3_API
  209.     AX_UnregisterClient(gAppData.ClientID);
  210. #else
  211.     AX_Shutdown();
  212. #endif
  213. }
  214.  
  215. /*****************************************************************************
  216.     Name:            initae
  217.     Description:    Install Event Handlers.
  218. *****************************************************************************/
  219. int    initae (void)
  220. {
  221.     long    result;
  222.     Boolean    HasAppleEvents;
  223.  
  224.     HasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? FALSE : result != 0);
  225.  
  226.     if (!HasAppleEvents) return -2;
  227.  
  228.     /* required events */
  229.     AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, 
  230.                            UPP (DoAEOpenApplication, AEEventHandler),
  231.                            0, FALSE);
  232.     AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, 
  233.                            UPP (DoAEQuitApplication, AEEventHandler),
  234.                            0, FALSE);
  235.     AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, 
  236.                            UPP (DoAEOpenOrPrintDocuments, AEEventHandler),
  237.                            kAEOpenDocuments, FALSE);
  238.     AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, 
  239.                            UPP (DoAEOpenOrPrintDocuments, AEEventHandler),
  240.                            kAEPrintDocuments, FALSE);
  241.  
  242.  
  243.     return noErr;
  244. }    /* end install event handlers */
  245.  
  246. /*****************************************************************************
  247.     DoAEOpenApplication
  248. *****************************************************************************/
  249. static pascal OSErr
  250. DoAEOpenApplication
  251.     (AppleEvent    *theAppleEvent,
  252.      AppleEvent    reply,
  253.      long        refcon)
  254. {
  255. #pragma unused(reply, refcon)
  256.     OSErr    error;
  257.  
  258.     error = MissedAnyParameters (theAppleEvent);
  259.  
  260.     CreateDocument(&gAppData.Documents, "\pUntitled");
  261.  
  262.     return error;
  263. }    /* DoAEOpenApplication */
  264.  
  265. /*****************************************************************************
  266.     DoAEQuitApplication
  267. *****************************************************************************/
  268. static pascal OSErr
  269. DoAEQuitApplication
  270.     (AppleEvent    *theAppleEvent,
  271.      AppleEvent    reply,
  272.      long        refcon)
  273. {
  274. #pragma unused(theAppleEvent, refcon)
  275.     OSErr    error = noErr;
  276.     
  277.     gAppData.Quitting = true;
  278.     
  279.     AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger,
  280.                    (Ptr) &error, sizeof (OSErr));
  281.     return noErr;
  282. }    /* DoAEQuitApplication */
  283.  
  284. /*****************************************************************************
  285.     DoAEOpenOrPrintDocuments
  286. *****************************************************************************/
  287. static pascal OSErr
  288. DoAEOpenOrPrintDocuments
  289.     (AppleEvent    *theAppleEvent,
  290.      AppleEvent    reply,
  291.      long        refcon)        /* SHOULD be kAEPrintDocuments or kAEOpenDocuments */
  292. {
  293. #pragma unused(reply, refcon)
  294.     OSErr        error;
  295.     AEDescList    docList;
  296.     DescType    actualType;
  297.     long        actualSize;
  298.     AEKeyword    actualEventID;
  299.  
  300.     docList.dataHandle = NULL;
  301.  
  302.     /*
  303.         the following is a hack. For some reason the refcon I think SHOULD be passed in,
  304.         (that is, the eventID) is not being passed in. Therefore, I get the eventID directly
  305.         from the event and use it to determine if we are doing an Open or Print. [der: 4/17/95]
  306.     */
  307.     error = AEGetAttributePtr (theAppleEvent, keyEventIDAttr, typeType,
  308.                                &actualType, &actualEventID, sizeof (AEKeyword), &actualSize);
  309.     /* hack ends here */
  310.  
  311.     error = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList);
  312.     if (!error)
  313.     {
  314.         if (MissedAnyParameters (theAppleEvent) == noErr)
  315.         {
  316.             long    itemsInList;
  317.             error = AECountItems (&docList, &itemsInList);
  318.             if (!error)
  319.             {
  320.                 FSSpec        theFSS;
  321.                 AEKeyword    ignoredKeyWord;
  322.                 DescType    ignoredType;
  323.                 Size        ignoredSize;
  324.                 short        index;
  325.  
  326.                 for (index = 1; index <= itemsInList; ++index)
  327.                 {
  328.                     FInfo    vFinderInfo;
  329.                     
  330.                     error = AEGetNthPtr (&docList, index, typeFSS, &ignoredKeyWord,
  331.                                          &ignoredType, (Ptr) &theFSS, sizeof (FSSpec), &ignoredSize);
  332.                     if (error) break;
  333.                     
  334.                     if (FSpGetFInfo(&theFSS, &vFinderInfo) != noErr)
  335.                         break;
  336.                     
  337.                     switch (vFinderInfo.fdType)
  338.                     {
  339.                         case 'SmpC':
  340.                         {
  341.                             EventRecord        Event;
  342.                             
  343.                             EventAvail(everyEvent, &Event);
  344.                             DoOpenDoc(&theFSS);
  345.                             break;
  346.                         } // FILETYPE
  347.                         
  348.                     } // switch
  349.                     
  350.                 } // for
  351.             } // if
  352.         } // if
  353.         
  354.         (void) AEDisposeDesc (&docList);
  355.     }
  356.     return error;
  357. }    /* DoAEOpenDocuments */
  358.  
  359.  
  360. /*****************************************************************************
  361.     MissedAnyParameters
  362. *****************************************************************************/
  363. static OSErr
  364. MissedAnyParameters
  365.     (const AppleEvent    *theAppleEvent)
  366. {
  367.     OSErr    error = noErr;
  368.     DescType    actualType;
  369.     long        actualSize;
  370.  
  371.     if (AESizeOfAttribute (theAppleEvent, keyMissedKeywordAttr, &actualType, &actualSize) != errAEDescNotFound)
  372.     {
  373.         error = errAEParamMissed;
  374.     }
  375.  
  376.     return error;
  377. }    /* MissedAnyParameters */
  378.  
  379.